
package com.servequake.truemusic.applet;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.util.*;
import java.net.*;
import java.io.*;

public class TrueMusicApplet extends JApplet implements ActionListener,ScrollPaneConstants,Runnable, MouseListener, ChangeListener
{
	static TrueMusicApplet inst;

	

	AppletAudio current;
	Vector vec = new Vector();
	int cursel = 0;

	JLabel version = new JLabel("TrueMusicApplet 20050417b",JLabel.CENTER);
	JLabel track = new JLabel("<no audio loaded>");
	JButton back = new JButton("<<");
	JButton play = new JButton(">");
	JButton pause = new JButton("||");
	JButton stop = new JButton("X");
	JButton next = new JButton(">>");
	JSlider prog = new JSlider(0,1);
	JSlider vol = new JSlider(-80,0,-20);
	JList list = new JList();	


	public void init()
	{
		inst = this;

		version.setFont( version.getFont().deriveFont(Font.BOLD) );

		back.addActionListener(this);
		play.addActionListener(this);
		pause.addActionListener(this);
		stop.addActionListener(this);
		next.addActionListener(this);

		list.addMouseListener(this);

		prog.addChangeListener(this);


		play.setToolTipText("Play");
		stop.setToolTipText("Stop");
		pause.setToolTipText("Pause");
		next.setToolTipText("Next Track");
		back.setToolTipText("Previous Track");
		prog.setToolTipText("Seek");
		vol.setToolTipText("Volume");
		track.setToolTipText("Current Track");
		

		Dimension d = vol.getPreferredSize();
		vol.setPreferredSize( new Dimension(60,d.height) );

		JPanel row = new JPanel();
		row.add(back);
		row.add(pause);
		row.add(play);
		row.add(stop);
		row.add(next);

		JPanel info = new JPanel();
		info.setLayout(new BorderLayout());
		info.add(vol, BorderLayout.EAST);
		info.add(track);


		JPanel top = new JPanel();
		top.setLayout(new GridLayout(4,1));
		top.add(version);
		top.add(info);
		top.add(row);
		top.add(prog);


		JPanel all = new JPanel();
		all.setLayout(new BorderLayout());
		all.setBorder( BorderFactory.createTitledBorder("") );
		all.add( top, BorderLayout.NORTH );
		all.add( new JScrollPane(list, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_NEVER) );

		getContentPane().add(all);			


		validate();
		setVisible(true);

		new Thread(this).start();
	}

	public void destroy()
	{
		for(int i=0;i<vec.size();i++)
		{
			AppletAudio a = (AppletAudio) vec.get(i);
			a.dispose();
		}

		vec = null;

		if(inst==this) inst=null;
	}

	public void stateChanged(ChangeEvent e) 
	{
		current.setPos(prog.getValue());
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==play) play();
		if(e.getSource()==pause) pause();
		if(e.getSource()==stop) stop2();
		if(e.getSource()==next) next();
		if(e.getSource()==back) back();
	}


	public void check()
	{
		track.setText(current.toString());
		repaint();
	}

	public void play()
	{
		current.play();
		check();
	}

	public void stop2()
	{
		current.stop();
		check();
	}

	public void pause()
	{
		current.pause();
		check();
	}

	public void next()
	{
		cursel++;

		if(cursel>=vec.size())
		{
			cursel--;
			stop2();
		}
		else
		{
			current.stop();
			current = (AppletAudio)vec.get(cursel);
			current.play();
		}
		check();
	}

	public void back()
	{
		cursel--;

		if(cursel<0)
		{
			cursel++;
			stop2();
		}
		else
		{
			current.stop();
			current = (AppletAudio)vec.get(cursel);
			current.play();
		}
		check();
	}

	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}

	public void mouseClicked(MouseEvent e)
	{
		if(e.getClickCount()==2) //double click: play that item
		{
			int index = list.locationToIndex(e.getPoint());
			if(index==-1) return;

			if(index==cursel)
			{
				current.play();
				return;
			}

			cursel=index;

			current.stop();
			current = (AppletAudio)vec.get(index);
			current.play();
		}
		check();	
	}


	public void run()
	{
		try
		{

			URL playlist = new URL( getParameter("playlist") );

			BufferedReader in = new BufferedReader( new InputStreamReader( playlist.openStream() ) );

			while(true)
			{
				String track = in.readLine();
				String artist = in.readLine();
				String url = in.readLine();

				if(track==null||artist==null||url==null)break;

				AppletAudio a = new AppletAudio(track,artist, new URL(url) );
				vec.add(a);
				list.setListData(vec);
				if(current==null)
				{
					current = a;
					check();
				}
				repaint();
			}

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	static
	{
	      JFrame.setDefaultLookAndFeelDecorated(true);

		try{
			UIManager.setLookAndFeel( (LookAndFeel) Class.forName("com.sun.java.swing.plaf.gtk.GTKLookAndFeel").newInstance() );
		}catch(Throwable e){}
		try{
			UIManager.setLookAndFeel( (LookAndFeel) Class.forName("com.sun.java.swing.plaf.mac.MacLookAndFeel").newInstance() );
		}catch(Throwable e){}
		try{
			UIManager.setLookAndFeel( (LookAndFeel) Class.forName("com.sun.java.swing.plaf.windows.WindowsLookAndFeel").newInstance() );
		}catch(Throwable e){}
		try{
			UIManager.put("AuditoryCues.playList",
			UIManager.get("AuditoryCues.allAuditoryCues"));
		}catch(Throwable e){}
	}
}
